createdQuestion = "";
question = "";
categorieQuestion = "";
update = false;
function getParameterByName(name)
{
  name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
  var regexS = "[\\?&]" + name + "=([^&#]*)";
  var regex = new RegExp(regexS);
  var results = regex.exec(window.location.search);
  if(results == null)
    return "";
  else
    return decodeURIComponent(results[1].replace(/\+/g, " "));
}

$(document).ready(
	function(){
		
		jQuery("#frmCreerQuestion").validationEngine();
		var ddlcategorieQuestionOptions = $("#ddlCategorieQuestion");
		var txtContenuQuestion = $("#txtContenuQuestion");
		
		var data = {
				idQuestion : getParameterByName("idQuestion")
			};
		
		if(data.idQuestion.length > 0){
			update = true;
			$.ajax({
				type : 'Get',
				url : '/qRH-WS/services/question/getQuestionById',
				async: false,
				dataType : 'json',
				data : data,
				crossDomain : true,
				success : function(result, textStatus, jqXHR) {
					question = result;
					categorieQuestion = question.categorieDto.libelleCategorie;
					txtContenuQuestion.text(question.contenuQuestion);
					
				},
				error : function(xhr, ajaxOptions, thrownError) {
					alert(xhr.status);
					alert(xhr.responseText);
				}
			});
		}
		
		$.ajax({
			type : 'Get',
			url : '/qRH-WS/services/categorie/getAll',
			async: false,
			dataType : 'json',
			crossDomain : true,
			success : function(result, textStatus, jqXHR) {
				jQuery.each(result, function() {
					if(categorieQuestion == this.libelleCategorie){
						ddlcategorieQuestionOptions.append($("<option selected=selected />").val(this.libelleCategorie).text(this.libelleCategorie));
					} else {
						ddlcategorieQuestionOptions.append($("<option />").val(this.libelleCategorie).text(this.libelleCategorie));
					}
						 
				});   
			},
			error : function(xhr, ajaxOptions, thrownError) {
				alert(xhr.status);
				alert(xhr.responseText);
			}
		});
		
		creerOuModifierQuestion = function(){
			
			var question = {};
			
			var url = "";
			if(update){
				url = "/qRH-WS/services/question/update";
				question = {
						idQuestion: getParameterByName("idQuestion"),
			            contenuQuestion: txtContenuQuestion.val(),
			            categorie:ddlcategorieQuestionOptions.val()
			    };
			} else {
				url = "/qRH-WS/services/question/create";
				question = {
			            contenuQuestion: txtContenuQuestion.val(),
			            categorie:ddlcategorieQuestionOptions.val()
			    };
			}
			
			if(txtContenuQuestion.val().length > 0){
				$.ajax({
					type : 'Get',
					url : url,
					dataType : 'json',
					data : question,
					crossDomain : true,
					success : function(result, textStatus, jqXHR) {
						createdQuestion = result; 
						window.location.replace("addReponseEventuelleToQuestion.html?idQuestion="+createdQuestion.idQuestion);
					},
					error : function(xhr, ajaxOptions, thrownError) {
						alert(xhr.status);
						alert(xhr.responseText);
					}
				});
			}
		};
	}
);